//
//  SoundRecordingView.m
//  MelonBaseProject
//
//  Created by 葛悦彣 on 2020/6/16.
//  Copyright © 2020 Melon. All rights reserved.
//

#import "SoundRecordingView.h"
#import "SpectrumView.h"
#import "AudioSpectrumRecorder.h"
@interface SoundRecordingView()<AudioSpectrumRecorderDelegate>

@property (nonatomic, strong) AudioSpectrumRecorder *recorder;
@property (nonatomic, strong) SpectrumView *spectrumView;
@property (nonatomic ,strong) UIImageView * qxBtn;
@property (nonatomic ,strong) UIImageView * buttonView;
@property (nonatomic ,strong) UIImageView * yuyin;
@property (nonatomic ,strong) UIImageView * wenzi;

@property (nonatomic ,strong) UILabel * wenziLab;
///用来点击取消的view
@property (nonatomic ,strong) UIView * qxView;
///用来点击发送语言的view
@property (nonatomic ,strong) UIView * yyView;
@property (nonatomic ,strong)UILabel * timelab;
@property (nonatomic,strong) NSTimer * timer;

@end

@implementation SoundRecordingView
/*
 
 UIControlEventTouchUpOutside
 指鼠标在控件边界范围外抬起，前提先得按下，然后拖动到控件外，即 UIControlEventTouchDown -> UIControlEventTouchDragInside(n 个) -> UIControlEventTouchDragExit -> UIControlEventTouchDragOutside(n 个) 时间序列，再然后就是抬起鼠标，产生UIControlEventTouchUpOutside事件。
 
 */
//
+ (instancetype)NewSoundRecordingView{
    SoundRecordingView * view = [SoundRecordingView new];
    view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
//    view.qxBtn = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"temp"]];
//    view.qxBtn.frame = CGRectMake(Kwt(45), SCREEN_HEIGHT  - Kwt(214), Kwt(70), Kwt(70));
////    view.qxBtn.backgroundColor = UIColor.redColor;
//    [view addSubview:view.qxBtn];
    
//    view.buttonView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"temp"]];
//    //    view.buttonView.backgroundColor = UIColor.orangeColor;
//    view.buttonView.frame = CGRectMake(0, SCREEN_HEIGHT  - Kwt(100), SCREEN_WIDTH,  Kwt(100));
//    [view addSubview:view.buttonView];
    view.spectrumView = [[SpectrumView alloc] initWithFrame:CGRectMake(Kwt(100),  SCREEN_HEIGHT  - Kwt(340), Kwt(175), 60)];
    [view addSubview:view.spectrumView];
    [DTKTool YJBK:kwt(5) andView:view.spectrumView andWidth:1 andColor:KcolorHex(@"#40BB6a")];
    view.spectrumView.backgroundColor = KcolorHex(@"#29B560");

    
    UILabel * timelab = [DTKTool labelWithText:@"0.00" andTextColor:UIColor.whiteColor andFont:kFont(15)];
    view.timelab = timelab;
    [view.spectrumView addSubview:timelab];
    [timelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.bottom.offset(-10);
    }];
//    view.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:view selector:@selector(voiceChange:) userInfo:nil repeats:YES];

//    view.wenzi = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"temp"]];
//    view.wenzi.frame = CGRectMake(Kwt(260), SCREEN_HEIGHT  - Kwt(214), Kwt(70), Kwt(70));
//    [view addSubview:view.wenzi];
//
//    
//    view.yuyin = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"temp"]];
//    view.yuyin.frame = CGRectMake(Kwt(175), Kwt(50), Kwt(25),Kwt(25));
//    [view.buttonView addSubview:view.yuyin];
    
    //    view.wenziLab = [UILabel labelWithHexColor:0x040404 fontSize:Kwt(15) text:@""];
//    view.wenziLab = [DTKTool labelWithText:@"" andTextColor:KcolorHex(@"0x333333") andFont:kFont(15)];
//    view.wenziLab.textAlignment = NSTextAlignmentCenter;
//    view.wenziLab.numberOfLines = 3;
//    view.wenziLab.frame = CGRectMake(Kwt(20),  SCREEN_HEIGHT  - Kwt(400), Kwt(335), Kwt(100));
//    [view addSubview:view.wenziLab];
//    view.wenziLab.backgroundColor = [UIColor colorWithHexString:@"#D5F4FC"];
//    view.wenziLab.layer.masksToBounds = YES;
//    view.wenziLab.layer.cornerRadius = Kwt(5);
//    view.wenziLab.hidden = YES;
//    view.qxView = [UIView new];
//    view.qxView.hidden = YES;
//    view.qxView.backgroundColor = [UIColor clearColor];
//    view.qxView.userInteractionEnabled = YES;
//    view.qxView.frame = CGRectMake(Kwt(45), SCREEN_HEIGHT  - Kwt(210), Kwt(60), Kwt(60));
//    [view.qxView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:view action:@selector(openqx)]];
//
//    UIImageView * qximg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yuYInEndFH"]];
//    qximg.frame = CGRectMake(Kwt(17), 0, Kwt(26), Kwt(26));
//    [view.qxView addSubview:qximg];
//    UILabel * qxlab =  [DTKTool labelWithText:@"取消" andTextColor:KcolorHex(@"0xffffff") andFont:kFont(55)];
//    qxlab.textAlignment = NSTextAlignmentCenter;
//    qxlab.frame = CGRectMake(Kwt(0), Kwt(40), Kwt(60), Kwt(20));
//    [view.qxView addSubview:qxlab];
//    [view addSubview:view.qxView];
//    
//    view.yyView = [UIView new];
//    view.yyView.hidden = YES;
//    view.yyView.backgroundColor = [UIColor clearColor];
//    view.yyView.userInteractionEnabled = YES;
//    [view.yyView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:view action:@selector(openyy)]];
//    view.yyView.frame = CGRectMake(SCREEN_WIDTH / 2 - Kwt(30), SCREEN_HEIGHT  - Kwt(210), Kwt(60), Kwt(60));
//    UIImageView * yyimg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yuYInIcon"]];
//    yyimg.frame = CGRectMake(Kwt(17), 0, Kwt(26), Kwt(26));
//    [view.yyView addSubview:yyimg];
//    UILabel * yylab = [UILabel labelWithHexColor:UIColor.whiteColor fontSize:Kwt(14) text:@"发送原语音"];
//    yylab.textAlignment = NSTextAlignmentCenter;
//    yylab.frame = CGRectMake(Kwt(-20), Kwt(40), Kwt(100), Kwt(20));
//    [view.yyView addSubview:yylab];
//    [view addSubview:view.yyView];
    [view openTime];
    [view.recorder startRecord];
    return view;
}
- (void)openTime{
    if (_timer.isValid) {//判断timer是否在线程中
        [_timer invalidate];
    }
    if(!_timer){
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(voiceChange:) userInfo:nil repeats:YES];
    }
}
- (void)newTime{
    if (_timer.isValid) {//判断timer是否在线程中
        [_timer invalidate];
    }
    self.timeS = 0;
    self.timelab.text = @"0:00";
    _timer=nil;
}
- (void)setTimeS:(int)timeS{
    _timeS = timeS;
    self.timelab.text = [NSString stringWithFormat:@"0:00"];
}
-(void)voiceChange:(NSTimer *)timer{
    self.timeS ++;
    if(self.timeS> 9){
        self.timelab.text = [NSString stringWithFormat:@"0:%d",self.timeS];

    } else {
        self.timelab.text = [NSString stringWithFormat:@"0:0%d",self.timeS];

    }
    if(self.timeS > 30){
        if (_timer.isValid) {//判断timer是否在线程中
            [_timer invalidate];
        }
        self.timeS = 0;
        if(self.ovewBlock){
            self.ovewBlock();
        }
        _timer=nil;
        return;
    }
    
}
#pragma mark - AudioSpectrumRecorderDelegate
- (void)recorderDidGenerateSpectrum:(NSArray *)spectrums {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.spectrumView updateSpectra:spectrums withStype:ADSpectraStyleRound];
    });
}

- (AudioSpectrumRecorder *)recorder {
    if (!_recorder) {
        _recorder = [[AudioSpectrumRecorder alloc] init];
        _recorder.delegate = self;
    }
    return _recorder;
}
-(void)setSbStr:(NSString *)sbStr {
    _sbStr = sbStr;
    self.wenziLab.text = sbStr;
    if(sbStr.length < 1){
        self.wenziLab.hidden = YES;
    } else {
        self.wenziLab.hidden = NO;
    }
}
-(void)setIsClose:(BOOL)isClose {
    _isClose = isClose;
//    if(isClose){
//        self.buttonView.image = [UIImage imageNamed:@"yuYInBottomClose"];
//        self.qxBtn.image = [UIImage imageNamed:@"yuYInEnd"];
//        self.yuyin.image = [UIImage imageNamed:@"yuYInIcon"];
//    } else{
//        self.buttonView.image = [UIImage imageNamed:@"yuYInBottom"];
//        self.qxBtn.image = [UIImage imageNamed:@"yuYInEndClose"];
//        self.yuyin.image = [UIImage imageNamed:@"yuYInIconClose"];
//
//    }
}

-(void)setType:(SoundRecordingViewType)type {
    _type = type;
    self.spectrumView.hidden = NO;
    self.qxView.hidden = YES;
    self.yyView.hidden = YES;
    self.buttonView.hidden = NO;
    self.qxBtn.hidden = NO;
    self.yuyin.hidden = NO;
    switch (type) {
        case SoundRecordingViewTypeEnd:
            self.buttonView.image = [UIImage imageNamed:@"yuYInBottom"];
            self.qxBtn.image = [UIImage imageNamed:@"yuYInEndClose"];
            self.yuyin.image = [UIImage imageNamed:@"yuYInIconClose"];
            self.wenzi.image = [UIImage imageNamed:@"yuYInWZEndClose"];
            break;
        case SoundRecordingViewTypeUnknown:
            self.buttonView.image = [UIImage imageNamed:@"yuYInBottomClose"];
            self.qxBtn.image = [UIImage imageNamed:@"yuYInEnd"];
            self.yuyin.image = [UIImage imageNamed:@"yuYInIcon"];
            self.wenzi.image = [UIImage imageNamed:@"yuYInWZEndClose"];
            break;
        case SoundRecordingViewTypeWz:
            self.buttonView.image = [UIImage imageNamed:@"yuYInBottomClose"];
            self.qxBtn.image = [UIImage imageNamed:@"yuYInEndClose"];
            self.yuyin.image = [UIImage imageNamed:@"yuYInIcon"];
            self.wenzi.image = [UIImage imageNamed:@"yuYInWZEnd"];
            break;
        case SoundRecordingViewTypeWzEnd:
            self.buttonView.hidden = YES;
            self.qxBtn.hidden = YES;
            self.yuyin.hidden = YES;
            self.wenzi.image = [UIImage imageNamed:@"yuYInEndOK"];
            self.wenzi.userInteractionEnabled = YES;
            [self.wenzi addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openWz)]];
            self.spectrumView.hidden = YES;
            self.qxView.hidden = NO;
            self.yyView.hidden = NO;

            //还有2个按钮
            break;
        default:
            break;
    }
}

- (void)openWz{
    NSLog(@"发送文字出去");
    if(self.wzBlock){
        self.wzBlock();
    }
}
- (void)openqx{
    NSLog(@"取消");
    if(self.qxBlock){
        self.qxBlock();
    }
}
- (void)openyy{
    NSLog(@"发送语言出去");
    if(self.yyBlock){
        self.yyBlock();
    }
}
//- (void)panAction:(UIPanGestureRecognizer *)pan{
//    NSLog(@"拖拽");
//    CGPoint point = [pan translationInView:pan.view];
//    NSLog(@"当前位置:%f%f",point.x,point.y);
//}
//
//
//- (void)UIControlEventTouchDragEnter{
//    NSLog(@"指拖动动作中，从控件边界外到内时产生的事件");
//}
//- (void)UIControlEventTouchDragExit{
//    NSLog(@"指拖动动作中，从控件边界内到外时产生的事件");
//
//}
//- (void)UIControlEventTouchUpOutside{
//    NSLog(@"XXXXXXXXXXXXXXXXXXXX");
//}
//- (void)UIControlEventTouchUpInside{
//    NSLog(@"指鼠标在控件范围内抬起，前提先得按下，即UIControlEventTouchDown或UIControlEventTouchDownRepeat事件");
//}
////- (void)UIControlEventTouchUpInside{
////    NSLog(@"UIControlEventTouchUpInside");
////
////}
@end
